package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.facade.cda.dto.ReqDto;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.ZhuYuanZhaiYaoService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.infra.cda.zyzy.*;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.ValueUnit;
import com.jy.rhin.model.ZhuYuanZY;
import com.jy.rhin.model.body.feiyong.FeiYongTM;
import com.jy.rhin.model.body.feiyong.ZhuYuanFeiYong;
import com.jy.rhin.model.body.huizhenyijian.HuiZhenYiJian;
import com.jy.rhin.model.body.huizhenyuanyin.HuiZhenYuanYin;
import com.jy.rhin.model.body.shiyanshijiancha.ShiYanShiJCJY;
import com.jy.rhin.model.body.shoushucaozuo.ShouShuMaZui;
import com.jy.rhin.model.body.yongyao.YongYao;
import com.jy.rhin.model.body.zhenduanjilu.ZhenDuan;
import com.jy.rhin.model.body.zhengzhuang.ZhengZhuang;
import com.jy.rhin.model.body.zhiliaojihua.ZhiLiaoJH;
import com.jy.rhin.model.body.zhuanzhenjianyi.ZhuanZhenJY;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2023/2/22
 * @Description
 */
@Service
public class ZhuYuanZhaiYaoApplication {

    @Resource
    private ZhuYuanZhaiYaoService zhuYuanZhaiYaoService;
    @Resource
    private PatientService patientService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;
    @Resource
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(ReqDto req) {
        return cdaCreator.create(createZhuYuanZhaiYao(req), "18zhuyuanzhaiyao.ftl");
    }

    public ClinicalDocumentData createZhuYuanZhaiYao(ReqDto req) {
        ZhuYuanInfoVo zhuYuanInfoVo = zhuYuanZhaiYaoService.queryZhuYuanInfo(req);
        if (Objects.isNull(zhuYuanInfoVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        BingRenXXDto bingRenInfo = patientService.queryPatientById(zhuYuanInfoVo.getPatientId());
        if (Objects.isNull(bingRenInfo)) {
            throw new CDAException("患者信息不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(zhuYuanInfoVo.getYiLiaoJGDM()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(zhuYuanInfoVo.getYiLiaoJGDM()).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        //前置数据齐备，再来处理CDA
        ZhuYuanZhaiYaoVo zhuYuanZhaiYaoVo = new ZhuYuanZhaiYaoVo();
        zhuYuanZhaiYaoService.zhuYuanZhaiYao(req, zhuYuanZhaiYaoVo);
        shuYuZhiYuQuery(zhuYuanZhaiYaoVo, zhuYuanInfoVo);

        ZhuYuanZY docData = new ZhuYuanZY();
        docData.setId(String.valueOf(zhuYuanInfoVo.getId()));
        setClinicalDocumentData(docData, zhuYuanInfoVo, bingRenInfo, hospital);

        ComponentOf componentOf = new ComponentOf();
        componentOf.setEffectiveTime(CDAUtil.toD15(zhuYuanInfoVo.getGengXinSJ()));
        componentOf.setRepresentedOrganizationName(zhuYuanInfoVo.getRuYuanKSMC());
        componentOf.setWholeOrganizationId(CDAUtil.toJGID10(hospital.getHospitalId()));
        componentOf.setWholeOrganizationName(hospital.getName());
        docData.setComponentOf(componentOf);
        docData.setHuiZhenYY("身体不舒服需要会诊");
        //症状
        List<ZhengZhuang> zhengZhuangList = new ArrayList<>();
        zhengZhuangList.add(zhuYuanInfoVo.zhuYuanInfoVo());
        docData.setZhengZhuangList(zhengZhuangList);
        //实验室检查检验
        List<ShiYanShiJCJY> shiYanShiJCJYList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(zhuYuanZhaiYaoVo.getJianChaInfoList())) {
            shiYanShiJCJYList.addAll(zhuYuanZhaiYaoVo.getJianChaInfoList().stream().map(JianChaInfoVo::zhuYuanJianChaVo).collect(Collectors.toList()));
            docData.setShiYanShiJCJYList(shiYanShiJCJYList);

        }
        if (!CollectionUtils.isEmpty(zhuYuanZhaiYaoVo.getJianYanInfoList())) {
            shiYanShiJCJYList.addAll(zhuYuanZhaiYaoVo.getJianYanInfoList().stream().map(JianYanInfoVo::zhuYuanJianYanVo).collect(Collectors.toList()));
            docData.setShiYanShiJCJYList(shiYanShiJCJYList);
        }

        //诊断
        List<ZhenDuan> zhenDuanList = new ArrayList<>();
        ZhenDuan zhenDuan = new ZhenDuan();
        zhenDuan.setQueZhenSJ(CDAUtil.toD8(zhuYuanInfoVo.getRuYuanSJ()));
        zhenDuan.setJiBingZTDM(new CodeObject("3", "一般"));
        //zhenDuan.setJiBingZTDM(new CodeObject(,));
        zhenDuan.setBingQingZGDM(new CodeObject(zhuYuanInfoVo.getBingQingZGDM(), zhuYuanInfoVo.getBingQingZGMC()));
        //zhenDuan.setBingQingZGDM(new CodeObject("1", "治愈"));
        zhenDuan.setChuYuanSJ(CDAUtil.toD15(zhuYuanInfoVo.getChuYuanSJ()));
        if (zhuYuanZhaiYaoVo.getZhenDuanInfo() != null) {
            zhenDuan.setZhenDuanMC(zhuYuanZhaiYaoVo.getZhenDuanInfo().getZhenDuanMC());
            zhenDuan.setZhenDuanDM(zhuYuanZhaiYaoVo.getZhenDuanInfo().getZhenDuanDM());
            if (!StringUtil.isNullOrWhiteSpace(zhuYuanZhaiYaoVo.getZhenDuanInfo().getChuanRanXBZ())) {
                zhenDuan.setChuanRanXBZ("1".equals(zhuYuanZhaiYaoVo.getZhenDuanInfo().getChuanRanXBZ()) ? "true" : "false");
            }
            if (!StringUtil.isNullOrWhiteSpace(zhuYuanZhaiYaoVo.getZhenDuanInfo().getSiWangZDDM())) {
                zhenDuan.setSiWangZDDM(new CodeObject(zhuYuanZhaiYaoVo.getZhenDuanInfo().getSiWangZDDM(), zhuYuanZhaiYaoVo.getZhenDuanInfo().getSiWangZDMC()));
                //zhenDuan.setSiWangZDDM(new CodeObject("A01.000","伤寒"));
            }
            if (zhuYuanZhaiYaoVo.getZhenDuanInfo().getSiWangSJ() != null) {
                zhenDuan.setSiWangSJ(CDAUtil.toD15(zhuYuanZhaiYaoVo.getZhenDuanInfo().getSiWangSJ()));
            }
            zhenDuan.setSiWangSJ(CDAUtil.toD15(zhuYuanInfoVo.getChuYuanSJ()));

        }
        zhenDuanList.add(zhenDuan);
        docData.setZhenDuanList(zhenDuanList);

        //会诊原因
        HuiZhenYuanYin huiZhenYuanYin = new HuiZhenYuanYin();
        //会诊意见
        HuiZhenYiJian huiZhenYiJian = new HuiZhenYiJian();
        if (zhuYuanZhaiYaoVo.getHuiZhenInfoVo() != null) {
            huiZhenYuanYin.setHuiZhenYY(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenYY());
            huiZhenYiJian.setHuiZhenYJ(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenYJ());
            huiZhenYiJian.setHuiZhenRQ(CDAUtil.toD8(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenRQ()));
            huiZhenYiJian.setHuiZhenDLSH(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenDLSH());
            huiZhenYiJian.setHuiZhenYSXM(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenYSXM());
            huiZhenYiJian.setShenQingKSMC(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getShenQingKSMC());
            HospitalDTO huiZhenHospital = hospitalAPI.info(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenYSSZJGDM()).getData();
//            HospitalDTO huiZhenHospital = dataMigrationAPI.getByHospitalId(zhuYuanZhaiYaoVo.getHuiZhenInfoVo().getHuiZhenYSSZJGDM()).getData();
            if (huiZhenHospital != null) {
                huiZhenYiJian.setHuiZhenYSSZJGDM(huiZhenHospital.getOutId());
                huiZhenYiJian.setHuiZhenYSSZJGMC(huiZhenHospital.getName());
            } else {
                huiZhenYiJian.setHuiZhenYSSZJGDM(CDAUtil.toJGID10(null));
                huiZhenYiJian.setHuiZhenYSSZJGMC("-");
            }
            docData.setHuiZhenYJ(huiZhenYiJian);
        }

        //用药
        List<YongYao> yongYaoList = new ArrayList<>();
        List<YongYao.YongYaoItem> yongYaoItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(zhuYuanZhaiYaoVo.getYongYaoList())) {
            for (ZhuYuanYZVo yz : zhuYuanZhaiYaoVo.getYongYaoList()) {
                YongYao yongYao = new YongYao();
                yongYao.addZhongYaoSYLB(new CodeObject("1", "未使用"));
                yongYaoItemList.add(yz.YongYaoItemVo());
                yongYao.setYongYaoItemList(yongYaoItemList);
                yongYaoList.add(yongYao);
            }
        } else {
            YongYao yongYao = new YongYao();
            yongYao.addZhongYaoSYLB(new CodeObject("1", "未使用"));
            YongYao.YongYaoItem defultitem = new YongYao.YongYaoItem();
            defultitem.setYongYaoTJ(new CodeObject("9", "其他用药途径"));
            defultitem.setDanCiYYJL(new ValueUnit("0.00", "mg"));
            defultitem.setYaoWuSYPL(new ValueUnit("0", "次/天"));
            defultitem.setYaoWuJX(new CodeObject("00", "原料"));
            defultitem.setYaoPinMC("无");
            defultitem.setYaoWuLX("无");
            defultitem.setYongYaoTS(new ValueUnit("1", "天"));
            defultitem.setYaoWuSYZJL(new ValueUnit("1.00", "mg"));
            defultitem.setYongYaoTZRQ(CDAUtil.defaultD15());
            yongYaoItemList.add(defultitem);
            yongYao.setYongYaoItemList(yongYaoItemList);
            yongYaoList.add(yongYao);
        }
        docData.setYongYao(yongYaoList);

        //手术
        ArrayList<ShouShuMaZui> shouShuList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(zhuYuanZhaiYaoVo.getShouShuInfoList())) {
            shouShuList.addAll(zhuYuanZhaiYaoVo.getShouShuInfoList().stream().map(ShouShuInfoVo::zhuYuanShouShuVo).collect(Collectors.toList()));
            docData.setShouShuList(shouShuList);
        }

        //其他医学处置
        docData.setQiTaYXCZList(Collections.singletonList("无"));

        //转诊建议
        if (zhuYuanZhaiYaoVo.getZhuanZhenInfoVo() != null) {
            docData.setZhuanZhenJY(zhuYuanZhaiYaoVo.getZhuanZhenInfoVo().zhuanZhenJYItemVo());
        }


        //健康评估
        if (zhuYuanZhaiYaoVo.getJianKangPingGuInfo() != null) {
            docData.setJianKangPG(zhuYuanZhaiYaoVo.getJianKangPingGuInfo().getRuYuanQK());
        }
        //治疗计划
        ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
        if (zhuYuanZhaiYaoVo.getJianKangPingGuInfo() != null) {
            zhiLiaoJH.setZhiLiaoFA(zhuYuanZhaiYaoVo.getJianKangPingGuInfo().getJinHouZLFA());
            docData.setZhiLiaoJH(zhiLiaoJH);
        }


        //健康指导
        List<String> jianKangZDList = new ArrayList<>();
        jianKangZDList.add("无");
        docData.setJianKangZDList(jianKangZDList);

        //费用
        List<ZhuYuanFeiYong> feiYongList = new ArrayList<>();
        List<FeiYongInfoVo> feiYongInfoList = zhuYuanZhaiYaoVo.getFeiYongInfoList();
        if (!CollectionUtils.isEmpty(feiYongInfoList)) {
            Map<String, List<FeiYongInfoVo>> listMap = feiYongInfoList.stream().collect(Collectors.groupingBy(FeiYongInfoVo::getJIESUANLSH));
            for (Map.Entry<String, List<FeiYongInfoVo>> entry : listMap.entrySet()) {
                ZhuYuanFeiYong feiYong = new ZhuYuanFeiYong();
                List<FeiYongInfoVo> voList = entry.getValue();

                Map<String, List<FeiYongInfoVo>> cwflMap = voList.stream().collect(Collectors.groupingBy(FeiYongInfoVo::getMINGXICWFL));
                List<FeiYongTM> feiYongTMS = new ArrayList<>();
                for (Map.Entry<String, List<FeiYongInfoVo>> fy : cwflMap.entrySet()) {
                    FeiYongTM item = new FeiYongTM();
                    item.setZhuYuanFYFLMC(fy.getValue().get(0).getMingXiFYMC());
//                    item.setZhuYuanFYFL(new CodeObject(fy.getKey(), fy.getValue().get(0).getMingXiFYMC()));
                    item.setZhuYuanFYFL(new CodeObject(voList.get(0).getYiLiaoFKFSDM(), voList.get(0).getYiLiaoFKFSMC()));
                    feiYongTMS.add(item);
                }
                feiYong.setFeiYongTMList(feiYongTMS);

                DecimalFormat decimalFormat = new DecimalFormat("#.00");
                feiYong.setZhuYuanFYJE(String.valueOf(decimalFormat.format(voList.stream().mapToDouble(FeiYongInfoVo::getMINGXIXMJE).sum())));
                feiYong.setZhuYuanFYJSFS(new CodeObject(voList.get(0).getZhiFuFSDM(), voList.get(0).getZhiFuFSMC()));
                feiYong.setYiLiaoFKFS(new CodeObject(voList.get(0).getYiLiaoFKFSDM(), voList.get(0).getYiLiaoFKFSMC()));
                feiYongList.add(feiYong);
            }
            docData.setFeiYongList(feiYongList);
            //throw new CDAException("住院费用数据缺失", null);
        }

        setDefaultZhuYuanZY(docData);
        return docData;
    }

    /**
     * @param zhuYuanZhaiYaoVo
     */
    private void shuYuZhiYuQuery(ZhuYuanZhaiYaoVo zhuYuanZhaiYaoVo, ZhuYuanInfoVo zhuYuanInfoVo) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        //费用
        List<FeiYongInfoVo> feiYongInfoList = zhuYuanZhaiYaoVo.getFeiYongInfoList();
        if (!CollectionUtils.isEmpty(feiYongInfoList)) {
            feiYongInfoList.forEach(o -> {
                o.setZhiFuFSDM(StringUtils.isEmpty(o.getZhiFuFSDM()) || Arrays.asList("09", "10", "11", "12").contains(o.getZhiFuFSDM()) ? "99" : o.getZhiFuFSDM());
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV07_10_005.getCode(), feiYongInfoList.stream().map(FeiYongInfoVo::getYiLiaoFKFSDM).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_MXCWLBZY.getCode(), feiYongInfoList.stream().map(FeiYongInfoVo::getMINGXICWFL).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_FYJSFS.getCode(), feiYongInfoList.stream().map(FeiYongInfoVo::getZhiFuFSDM).collect(Collectors.toSet()));
        }
        List<JianYanInfoVo> jianYanInfoVo = zhuYuanZhaiYaoVo.getJianYanInfoList();
        if (!CollectionUtils.isEmpty(jianYanInfoVo)) {
            jianYanInfoVo.forEach(o -> {
                o.setJieGuoDM("0".equals(o.getJianYanJGYCBZ()) ? "1" : "1".equals(o.getJianYanJGYCBZ()) ? "2" : "3");
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JCJYJG.getCode(), jianYanInfoVo.stream().map(JianYanInfoVo::getJieGuoDM).collect(Collectors.toSet()));
        }
        List<JianChaInfoVo> jianChaInfoVo = zhuYuanZhaiYaoVo.getJianChaInfoList();
        if (!CollectionUtils.isEmpty(jianChaInfoVo)) {
            jianChaInfoVo.forEach(o -> {
                o.setJieGuoDM("0".equals(o.getYiChangBZ()) ? "1" : "1".equals(o.getYiChangBZ()) ? "2" : "3");
            });
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JCJYJG.getCode(), jianChaInfoVo.stream().map(JianChaInfoVo::getJieGuoDM).collect(Collectors.toSet()));
        }
        List<ShouShuInfoVo> shouShuInfoList = zhuYuanZhaiYaoVo.getShouShuInfoList();
        if (!CollectionUtils.isEmpty(shouShuInfoList)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_103.getCode(), shouShuInfoList.stream().map(ShouShuInfoVo::getMaZuiFFDM).collect(Collectors.toSet()));
        }
        if (!StringUtil.isNullOrWhiteSpace(zhuYuanInfoVo.getBingQingZGDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_10_010.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(zhuYuanInfoVo.getBingQingZGDM()))));
        }
        if (!StringUtil.isNullOrWhiteSpace(zhuYuanInfoVo.getZhengZhuangDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.ICD_10.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(zhuYuanInfoVo.getZhengZhuangDM()))));
        }
        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));

        //费用
        if (!CollectionUtils.isEmpty(feiYongInfoList)) {
            feiYongInfoList.forEach(info -> {
                info.setMingXiFYMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_MXCWLBZY.getCode(), info.getMINGXICWFL(), valueByCode));
                info.setYiLiaoFKFSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV07_10_005.getCode(), info.getYiLiaoFKFSDM(), valueByCode));
                info.setZhiFuFSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_FYJSFS.getCode(), info.getZhiFuFSDM(), valueByCode));
            });
        }
        if (!CollectionUtils.isEmpty(jianYanInfoVo)) {
            jianYanInfoVo.forEach(info -> {
                info.setJieGuoMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JCJYJG.getCode(), info.getJieGuoDM(), valueByCode));
            });
        }
        if (!CollectionUtils.isEmpty(jianChaInfoVo)) {
            jianChaInfoVo.forEach(info -> {
                info.setJieGuoMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JCJYJG.getCode(), info.getJieGuoDM(), valueByCode));
            });
        }

        if (!CollectionUtils.isEmpty(shouShuInfoList)) {
            shouShuInfoList.forEach(info -> {
                info.setMaZuiFFMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_103.getCode(), info.getMaZuiFFDM(), valueByCode));
            });
        }
        if (!StringUtil.isNullOrWhiteSpace(zhuYuanInfoVo.getBingQingZGDM())) {
            zhuYuanInfoVo.setBingQingZGMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_10_010.getCode(), zhuYuanInfoVo.getBingQingZGDM(), valueByCode));
        }

        if (!StringUtil.isNullOrWhiteSpace(zhuYuanInfoVo.getZhengZhuangDM())) {
            zhuYuanInfoVo.setZhengZhuangMC(ZhiJiUtils.getValue(HealthDictionaryEnum.ICD_10.getCode(), zhuYuanInfoVo.getZhengZhuangDM(), valueByCode));
       }

    }

    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData, ZhuYuanInfoVo zhuYuanInfoVo, BingRenXXDto bingRenXXDto, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(zhuYuanInfoVo.getRuYuanSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(bingRenXXDto, zhuYuanInfoVo));
        clinicalDocumentData.setAuthor(createAuthor(zhuYuanInfoVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        return clinicalDocumentData;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(!StringUtil.isNullOrWhiteSpace(hospital.getOutId()) ? hospital.getOutId() : CDAUtil.toJGID10(hospital.getHospitalId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(ZhuYuanInfoVo ZhuYuanInfoVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(ZhuYuanInfoVo.getRuYuanSJ()));
        author.setId(ZhuYuanInfoVo.getZhuZhiYSDM());
        author.setName(ZhuYuanInfoVo.getZhuZhiYSXM());
        author.setOrganizationId(!StringUtil.isNullOrWhiteSpace(hospital.getOutId()) ? hospital.getOutId() : CDAUtil.toJGID10(hospital.getHospitalId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(BingRenXXDto bingRenXXDto, ZhuYuanInfoVo zhuYuanInfoVo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(bingRenXXDto.getEid()));
        patientRole.setBingAnHBS(zhuYuanInfoVo.getBingAnHao() + "");
        patientRole.setHuiZhengJLBBH("123");
        patientRole.setTelecom(bingRenXXDto.getShouJiHao());
        patientRole.setAddr(createAddr(bingRenXXDto));
        patientRole.setPatient(createPatient(bingRenXXDto));
        return patientRole;
    }

    Addr createAddr(BingRenXXDto bingRenXXDto) {
        Addr addr = new Addr();
        addr.setState(bingRenXXDto.getJuZhuDSFMC());
        addr.setCity(bingRenXXDto.getJuZhuDSMC());
        addr.setCounty(bingRenXXDto.getJuZhuDQMC());
        addr.setTownship(bingRenXXDto.getJuZhuDJDMC());
        addr.setStreetName(bingRenXXDto.getJuZhuDSQMC());
        addr.setHouseNumber(bingRenXXDto.getJuZhuDMPH());
        //todo
        addr.setPostalCode("110000");
        return addr;
    }

    Patient createPatient(BingRenXXDto bingRenXXDto) {
        Patient patient = new Patient();
        patient.setId(bingRenXXDto.getZhengJianHM());
        patient.setName(bingRenXXDto.getBingRenXM());
        if ("男".equals(bingRenXXDto.getXingBie())) {
            bingRenXXDto.setXingBie("男性");
        } else if ("女".equals(bingRenXXDto.getXingBie())) {
            bingRenXXDto.setXingBie("女性");
        }
        patient.setAdministrativeGender(new CodeObject(bingRenXXDto.getXingBieDM(), bingRenXXDto.getXingBie()));
        patient.setBirthTime(bingRenXXDto.getChuShengRQ().replaceAll("-", ""));
        if (!StringUtil.isNullOrWhiteSpace(bingRenXXDto.getHunYinZT()) && !"-".equals(bingRenXXDto.getHunYinZT())) {
            patient.setMaritalStatus(new CodeObject(bingRenXXDto.getHunYinZT(), bingRenXXDto.getHunYinZTMC()));
        }
        if (!StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuDM()) && !StringUtil.isNullOrEmpty(bingRenXXDto.getMinZuMC())) {
            patient.setEthnicGroup(new CodeObject(bingRenXXDto.getMinZuDM(), bingRenXXDto.getMinZuMC()));
        }
        return patient;
    }

    private void setDefaultZhuYuanZY(ZhuYuanZY zhuYuanZY) {
        if (CollectionUtils.isEmpty(zhuYuanZY.getZhengZhuangList())) {
            List<ZhengZhuang> zhengZhuangList = new ArrayList<>();
            ZhengZhuang zz = new ZhengZhuang();
            zz.setKaiShiRQ(CDAUtil.defaultD15());
            zz.setZhengZhuangDM("M50.201");
            zz.setZhengZhuangMC("颈椎间盘突出");
            zz.setZhengZhuangCXSJ("10");
            zhengZhuangList.add(zz);
            zhuYuanZY.setZhengZhuangList(zhengZhuangList);
        } else {
            for (ZhengZhuang zz : zhuYuanZY.getZhengZhuangList()) {
                if (StringUtil.isNullOrEmpty(zz.getZhengZhuangDM()) || "-".equals(zz.getZhengZhuangDM())) {
                    zz.setKaiShiRQ(CDAUtil.defaultD15());
                    zz.setZhengZhuangDM("M50.201");
                    zz.setZhengZhuangMC("颈椎间盘突出");
                    zz.setZhengZhuangCXSJ("10");
                }
            }
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getZhenDuanList())) {
            List<ZhenDuan> zhenDuanList = new ArrayList<>();
            ZhenDuan zd = new ZhenDuan();
            zd.setQueZhenSJ(CDAUtil.defaultD8());
            zd.setZhenDuanDM("M50.201");
            zd.setZhenDuanMC("颈椎间盘突出");
            zd.setChuanRanXBZ("false");
            zd.setJiBingZTDM(new CodeObject("3", "一般"));
            zd.setBingQingZGDM(new CodeObject("9", "其他"));
            zd.setChuYuanSJ("20231012102144");
            zd.setSiWangZDDM(new CodeObject("A01.000", "伤寒"));
            zd.setSiWangSJ(CDAUtil.defaultD15());
            zhenDuanList.add(zd);
            zhuYuanZY.setZhenDuanList(zhenDuanList);
        } else {
            for (ZhenDuan zd : zhuYuanZY.getZhenDuanList()) {
                if (StringUtil.isNullOrEmpty(zd.getZhenDuanDM()) || "-".equals(zd.getZhenDuanDM())) {
                    zd.setQueZhenSJ(CDAUtil.defaultD8());
                    zd.setZhenDuanDM("M50.201");
                    zd.setZhenDuanMC("颈椎间盘突出");
                    zd.setChuanRanXBZ("false");
                    zd.setJiBingZTDM(new CodeObject("3", "一般"));
                    zd.setBingQingZGDM(new CodeObject("9", "其他"));
                    zd.setChuYuanSJ("20231012102144");
                    zd.setSiWangZDDM(new CodeObject("A01.000", "伤寒"));
                    zd.setSiWangSJ(CDAUtil.defaultD15());
                }
            }
        }
        if (StringUtil.isEmpty(zhuYuanZY.getHuiZhenYY())) {
            zhuYuanZY.setHuiZhenYY("无");
        }
        if (zhuYuanZY.getHuiZhenYJ() == null || StringUtil.isEmpty(zhuYuanZY.getHuiZhenYJ().getHuiZhenYJ())) {
            HuiZhenYiJian huiZhenYJ = new HuiZhenYiJian();
            huiZhenYJ.setHuiZhenYJ("无");
            huiZhenYJ.setHuiZhenRQ(CDAUtil.defaultD8());
            huiZhenYJ.setHuiZhenDLSH("-");
            huiZhenYJ.setHuiZhenYSXM("-");
            huiZhenYJ.setShenQingKSMC("-");
            huiZhenYJ.setHuiZhenYSSZJGDM(CDAUtil.toJGID10(null));
            huiZhenYJ.setHuiZhenYSSZJGMC("-");
            zhuYuanZY.setHuiZhenYJ(huiZhenYJ);
        }

        if (CollectionUtils.isEmpty(zhuYuanZY.getShiYanShiJCJYList())) {
            List<ShiYanShiJCJY> shiYanShiJCJYList = new ArrayList<>();
            ShiYanShiJCJY jy = new ShiYanShiJCJY();
            jy.setXiangMuDM("00901");
            jy.setXiangMuMC("尿酸");
            jy.setJieGuoDM(new CodeObject("2", "正常"));
            ShiYanShiJCJY jc = new ShiYanShiJCJY();
            jc.setXiangMuDM("00404");
            jc.setXiangMuMC("肺部CT");
            jc.setJieGuoDM(new CodeObject("2", "正常"));
            shiYanShiJCJYList.add(jc);
            shiYanShiJCJYList.add(jy);
            zhuYuanZY.setShiYanShiJCJYList(shiYanShiJCJYList);
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getYongYao())) {
            zhuYuanZY.setYongYao(new ArrayList<>());
            YongYao yongYao = new YongYao();
            yongYao.addZhongYaoSYLB(new CodeObject("1", "未使用"));
            YongYao.YongYaoItem item = MenZhenZhaiYaoApplication.getYongYaoItem();
            yongYao.addYongYaoItem(item);
            zhuYuanZY.getYongYao().add(yongYao);
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getShouShuList())) {
            List<ShouShuMaZui> shouShuList = new ArrayList<>();
            ShouShuMaZui shouShu = new ShouShuMaZui();
            shouShu.setCaoZuoMC("头和颈部血管治疗性超声");
            shouShu.setShouShuCZ(new CodeObject("00.0100", "头和颈部血管治疗性超声"));
            shouShu.setCaoZuoSJ(CDAUtil.defaultD15());
            shouShu.setMaZuiFF(new CodeObject("3", "局部麻醉"));
            shouShu.setMaZuiFFMC("局部麻醉");
            shouShuList.add(shouShu);
            zhuYuanZY.setShouShuList(shouShuList);
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getQiTaYXCZList())) {
            List<String> qiTaYXCZList = new ArrayList<>();
            qiTaYXCZList.add("无");
            zhuYuanZY.setQiTaYXCZList(qiTaYXCZList);
        }
        if (zhuYuanZY.getZhuanZhenJY() == null) {
            ZhuanZhenJY zhuanZhenJY = new ZhuanZhenJY();
            zhuanZhenJY.setZhuanZhenBZ("false");
            zhuanZhenJY.setZhuanZhenJGDM(CDAUtil.toJGID10(null));
            zhuanZhenJY.setZhuanZhenJGMC("-");
            zhuanZhenJY.setZhuanZhenKSDM("-");
            zhuanZhenJY.setZhuanZhenKSMC("-");
            zhuanZhenJY.setZhuanZhenYY("-");
            zhuanZhenJY.setZhuanZhenRQ(CDAUtil.defaultD8());
            zhuanZhenJY.setJianKangWYPG("-");
            zhuYuanZY.setZhuanZhenJY(zhuanZhenJY);
        }
        if (StringUtil.isEmpty(zhuYuanZY.getJianKangPG())) {
            zhuYuanZY.setJianKangPG("无");
        }
        if (zhuYuanZY.getZhiLiaoJH() == null || zhuYuanZY.getZhiLiaoJH().getZhiLiaoFA() == null) {
            ZhiLiaoJH zhiLiaoJH = new ZhiLiaoJH();
            zhiLiaoJH.setZhiLiaoFA("无");
            zhiLiaoJH.setChuzhiJH("无");
            zhuYuanZY.setZhiLiaoJH(zhiLiaoJH);
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getJianKangZDList())) {
            List<String> jianKangZDList = new ArrayList<>();
            jianKangZDList.add("无");
            zhuYuanZY.setJianKangZDList(jianKangZDList);
        }
        if (CollectionUtils.isEmpty(zhuYuanZY.getFeiYongList())) {
            List<ZhuYuanFeiYong> feiYongList = new ArrayList<>();
            ZhuYuanFeiYong feiYong = new ZhuYuanFeiYong();
            feiYong.setZhuYuanFYJE("100.00");
            feiYong.setZhuYuanFYJSFS(new CodeObject("01", "现金"));
            feiYong.setYiLiaoFKFS(new CodeObject("07", "全自费"));

            List<FeiYongTM> feiYongTMList = new ArrayList<>();
            FeiYongTM tm = new FeiYongTM();
            tm.setZhuYuanFYFL(new CodeObject("07", "全自费"));
            tm.setZhuYuanFYFLMC("床位费");
            feiYongTMList.add(tm);
            feiYong.setFeiYongTMList(feiYongTMList);
            feiYongList.add(feiYong);
            zhuYuanZY.setFeiYongList(feiYongList);
        }
    }
}
